*set path to where .dta file is and where you want graphs stored.
*install sdcasepick, vcemway // Stata 17.0
local path V:\docs\WOD
use "`path'\jlev_data_compact.dta",clear


forvalues i=1/27{
cap drop __gr`i'
gen __gr`i'=.
replace __gr`i'=1 if jid==`i'
replace __gr`i'=0 if jid !=`i' & jid !=.
logit jbincert __gr`i'##(__usptr __acc __alc  __diss __ir __incompdec __civlib __hasac) ///
if (aot==39 & u_ifp ==0 & u_ca==1) | (jid >=10 & jid !=.)  ,nolog cluster(__dncat)
estimates store m1_j`i'
cap drop __prm1_j`i'
predict __prm1_j`i'
levelsof __jn if jid==`i' 
}


cap drop m1_justice-m1_effse

gen m1_justice=""
gen m1_var=""
gen m1_effect=.
gen m1_secdiff_p=.
gen m1_effub=.
gen m1_efflb=.
gen m1_effse=.



local vlist __incompdec __usptr __acc __alc  __diss __ir  __civlib __hasac



local n=1
foreach v of local vlist{
	forvalues i=1/27{ 
		local model m1_j`i'
		est restore `model'
		levelsof __jn if jid==`i',local(jjj) clean
		di "`v'"
		*if "`v'"=="__libdec" | "`v'"=="__civlib" {
		cap sdcasepick `v' if e(sample) , gr(__gr`i') second prvar(__pr`model') prlb(.1) prub(.2)
		*}
		*if "`v'"!="__libdec" & "`v'"!="__civlib" {
		*	cap sdcasepick `v' if e(sample) , gr(__gr`i') second prvar(__pr`model') prlb(.2) prub(.3)
		*}
		cap replace m1_justice="`jjj'" in `n'
		cap replace m1_var="`v'" in `n'
		cap replace m1_effect=`r(g1_est)' in `n'
		cap replace m1_secdiff_p=`r(sd_p)' in `n'
		cap replace m1_effub=`r(g1_ub)' in `n'
		cap replace m1_effub=1-`r(basevalue)' if `r(g1_ub)' > (1-`r(basevalue)') & `r(g1_ub)' !=.
		cap replace m1_efflb=`r(g1_lb)' in `n'
		cap replace m1_effse=`r(g1_se)' in `n'
		local n=`n'+1
	}
}

*summarize effects
bysort m1_var: summ m1_effect



*crude way of getting different symbols for sig and ns sec diffs:
cap drop m1_seffect m1_nseffect
gen m1_seffect=m1_effect if m1_secdiff_p <= .05
gen m1_nseffect=m1_effect if m1_secdiff_p > .05





*graphs:
local vlist __incompdec  __usptr __acc __alc __diss __ir  __civlib __hasac
foreach v of local vlist{
	graph dot m1_seffect m1_nseffect if m1_var=="`v'", over(m1_justice, sort(m1_effect)) ///
	title(Effect Size for `: variable label `v'', size(medium)) subtitle(From baseline of ~.15, size (medsmall)) /// 
	xsize(7) ysize(10) marker(1, mcolor(navy) msymbol(circle)) marker(2, mcolor(navy) msymbol(circle_hollow)) ///
	legend(off)
	graph export "`path'\m1_`v'bl15.png", as(png) replace
}







**********effect sizes as f(extremism/tenure)*********************************


logit jbincert c.__exp##(__usptr __acc __alc  __diss __ir  __incompdec  __civlib __hasac ) ///
c.__j5dist##(__usptr __acc __alc  __diss __ir  __incompdec  __civlib __hasac)  i.__term ///
if (aot==39 & u_ifp ==0 & u_ca==1) | __term==68  | __term==82  ,nolog cluster(__dncat) 
est store m4  
cap drop m4_prvar
predict m4_prvar



*some postestimation:
*cap drop m4_var-m4_secdiff_p

*gen m4_var=""
*gen m4_loxeffect=.
*gen m4_hixeffect=.
*gen m4_secdiff_p=.





*sdcasepick version:
foreach gvr of varlist __exp __j5dist{
	est restore m4
	cap drop m4_hcgv
	summ `gvr' if e(sample)==1, det
	gen m4_hcgv=0 if float(`gvr')==float(`r(p10)')
	replace m4_hcgv=1 if float(`gvr')==float(`r(p90)')
	local vlist __usptr __acc __alc __diss __ir  __civlib  __hasac __incompdec
	*local n=1
	foreach v of local vlist{
		*tsdce `v', groupvar(m4_hcgv) verb  max sec 
		sdcasepick `v', groupvar(m4_hcgv) sec prlb(.1) prub(.2) prvar(m4_prvar)
	}
}

*jackknife sdcasepick version:



cap drop __jkvar __jkcvar __jksecd __jkp __jkz __jki
gen __jkvar =""
gen __jkcvar=""
gen __jksecd=.
gen __jkp=.
gen __jkz=.
gen __jki=.


local n=1
qui forvalues i=1/27{
logit jbincert c.__exp##(__usptr __acc __alc  __diss __ir  __incompdec  __civlib __hasac ) ///
c.__j5dist##(__usptr __acc __alc  __diss __ir  __incompdec  __civlib __hasac)  i.__term ///
if jid !=`i' & ((aot==39 & u_ifp ==0 & u_ca==1) | __term==68  | __term==82)  ,nolog cluster(__dncat) 

//the following command sets e(sample) to "trick" Stata into using all justices'
//information for the POST-estimation, so 10th and 90th percentile (and, less
//relevantly here, sample size) stay constant across jackknifes
estimates esample: jbincert __exp  __j5dist __usptr __acc __alc  __diss __ir  __incompdec ///
 __civlib __hasac __dncat __term if (aot==39 & u_ifp ==0 & u_ca==1) | __term==68  | __term==82, replace

est store m4_jk`i'
cap drop m4_prvarjk`i'
predict m4_prvarjk`i' 

foreach gvr of varlist __exp __j5dist{
est restore m4_jk`i'
estimates esample //checks that sample set correctly.
cap drop m4_hcgvjk`i'
summ `gvr' if e(sample)==1, det 
gen m4_hcgvjk`i'=0 if float(`gvr')==float(`r(p10)')
replace m4_hcgvjk`i'=1 if float(`gvr')==float(`r(p90)')
local vlist __usptr __acc __alc __diss __ir  __civlib  __hasac __incompdec
*local n=1
foreach v of local vlist{
	*tsdce `v', groupvar(m4_hcgv) verb  max sec 
	sdcasepick `v', groupvar(m4_hcgvjk`i') sec prlb(.1) prub(.2) prvar(m4_prvarjk`i')
	replace __jkcvar="`gvr'" in `n'
	replace __jkvar="`v'" in `n'
	replace __jksecd=`r(sd_est)' in `n'
	replace __jkp=`r(sd_p)' in `n'
	replace __jkz=`r(sd_z)' in `n'
	replace __jki=`i' in `n'
	local n=`n'+1
}
}
}


*find min sec-diff and max p-value, over jackknifed justices, for each var:
sort __jkvar
by __jkvar : summ __jksecd __jkp if __jkcvar=="__exp", det
by __jkvar : summ __jksecd __jkp if __jkcvar=="__j5dist", det

cap drop __jkvar __jkcvar __jksecd __jkp __jkz __jki




**Robustness checks mentioned in fn:
*multiway clustering:

vcemway logit jbincert c.__exp##(__usptr __acc __alc  __diss __ir  __incompdec  __civlib __hasac ) ///
c.__j5dist##(__usptr __acc __alc  __diss __ir  __incompdec  __civlib __hasac)  i.__term ///
if (aot==39 & u_ifp ==0 & u_ca==1) | __term==68  | __term==82  ,nolog cluster(__dncat jid) 
est store m4djc  //for docket/justice clustering
cap drop m4djc_prvar
predict m4djc_prvar

foreach gvr of varlist __exp __j5dist{
est restore m4djc
cap drop m4djc_hcgv
summ `gvr' if e(sample)==1, det
gen m4djc_hcgv=0 if float(`gvr')==float(`r(p10)')
replace m4djc_hcgv=1 if float(`gvr')==float(`r(p90)')
local vlist __usptr __acc __alc __diss __ir  __civlib  __hasac __incompdec
*local n=1
foreach v of local vlist{
	*tsdce `v', groupvar(m4djc_hcgv) verb  max sec 
	sdcasepick `v', groupvar(m4djc_hcgv) sec prlb(.1) prub(.2) prvar(m4djc_prvar)
}
}



**rank-based version of experience/extremism
est restore m4
tempvar tv1 tv2 tv3
egen `tv1'=tag(jid) if e(sample)==1
egen `tv2'=rank(__exp) if `tv1'==1
egen `tv3'=rank(__j5dist) if `tv1'==1
summ `tv2' `tv3'
egen __rkexp=mode(`tv2') if e(sample)==1 ,by(jid)
egen __rkj5dist=mode(`tv3') if e(sample)==1, by(jid)


logit jbincert c.__rkexp##(__usptr __acc __alc  __diss __ir  __incompdec  __civlib __hasac ) ///
c.__rkj5dist##(__usptr __acc __alc  __diss __ir  __incompdec  __civlib __hasac)  i.__term ///
if jid !=12 & ((aot==39 & u_ifp ==0 & u_ca==1) | __term==68  | __term==82)  ,nolog cluster(__dncat) 
est store m4r  
cap drop m4r_prvar
predict m4r_prvar



foreach gvr of varlist __rkexp __rkj5dist{
est restore m4r
cap drop m4r_hcgv
summ `gvr' if e(sample)==1, det
gen m4r_hcgv=0 if float(`gvr')==float(`r(p10)')
replace m4r_hcgv=1 if float(`gvr')==float(`r(p90)')
local vlist __usptr __acc __alc __diss __ir  __civlib  __hasac __incompdec
*local n=1
foreach v of local vlist{
	*tsdce `v', groupvar(m4_hcgv) verb  max sec 
	sdcasepick `v', groupvar(m4r_hcgv) sec prlb(.1) prub(.2) prvar(m4r_prvar)
}
}










